#include "Global.h"
#include "Config.h"
#include "Common.h"
#include "ObjectUser.h"
#include "ObjectCar.h"
#include "ObjectPark.h"
#include "ObjectOrder.h"
#include "ObjectArea.h"
#include "ObjectInteraction.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/filesystem.hpp>

#include <boost/format.hpp>   
#include <boost/tokenizer.hpp>   
#include <boost/algorithm/string.hpp>  
#include <boost/lexical_cast.hpp>
#include <boost/make_shared.hpp>
#include <fstream>
#include <sstream>
#include "Config.h"
#include "DatabaseMysql.h"
#include <boost/foreach.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/assign.hpp>
using namespace boost::assign;
using namespace std;


namespace RisingStar
{

	Global::Global(void)
	{
		Values["配置方案目录"] = CONF_OBJECT_CFG_PATH;
		Values["字典表"] = CONF_DIC_PATH;
	}



	Global::~Global(void)
	{

	}
	void Global::Print()
	{
		map<string,string>::iterator lIter;
		cout<<"\t|------------------------------------------------------------------|"<<endl;
		cout<<"\t|变量名\t\t变量值"<<endl;
		cout<<"\t|------------------------------------------------------------------|"<<endl;
		for(lIter = Values.begin();lIter != Values.end();++lIter)
		{
			cout<<"\t|"<<lIter->first<<"\t"<<lIter->second<<endl;
			cout<<"\t|------------------------------------------------------------------|"<<endl;
		}
	}
	
	bool Global::GetSemType(string in,string& out,string cType)
	{
		if(SemType.find(cType) != SemType.end())
		{
			if(SemType[cType].find(in) != SemType[cType].end())
			{
				out = SemType[cType][in];
				return true;
			}
		}
		return false;
	}
	
	bool Global::GetFormula(string formulaType,string in,string& out)
	{
		if(FormulaMap.find(in) != FormulaMap.end())
		{
			if(FormulaMap[in].find(formulaType) != FormulaMap[in].end())
			{
				out = FormulaMap[in][formulaType];
				
				return true;
			}
			else
			{
				boost::throw_exception(runtime_error(
											(boost::format("公式类型有误:【%s】")%formulaType).str()));
			}
		}
		else
		{
			boost::throw_exception(runtime_error(
											(boost::format("公式操作符有误:【%s】")%in).str()));
		}
		
		return false;
	}
	bool Global::ImportDic(string dicPath)
	{
		using namespace boost::property_tree;
		
		bool ret = true;
		
		try 
		{			
			ptree root;			
			read_xml(dicPath,root);			
			
			ptree Workbook = root.get_child("Workbook");
			ptree::iterator rootlIter = Workbook.begin();
			for(; rootlIter != Workbook.end(); ++rootlIter)
			{				
				if(rootlIter->first == "Worksheet")
				{
					string tblName = rootlIter->second.get<string>("<xmlattr>.ss:Name","");
					if(tblName == "信号表")
					{
						ptree Table = rootlIter->second.get_child("Table");
						
						ptree::iterator RowlIter;
						map<long,string> head;

						map<string,long> propertIndex;
						propertIndex["对象"] = 0;
						propertIndex["ID"] = 0;
						propertIndex["对象编码"] = 0;
						propertIndex["类型"] = 0;
						propertIndex["必要的信号量"] = 0;
						propertIndex["可选的信号量"] = 0;
						
						propertIndex["字段名"] = 0;
						propertIndex["字段类型"] = 0;
						propertIndex["是否非空"] = 0;
						propertIndex["是否唯一"] = 0;
						propertIndex["是否关键字段"] = 0;
						propertIndex["是否自增"] = 0;
						
						int row = 0;
						
						list<map<string,string>*> dicRequestSemIDList;
						for(RowlIter = Table.begin();RowlIter != Table.end();++RowlIter)
						{
							try
							{
								if(RowlIter->first == "Row")
								{//
									//long index = RowlIter->second.get<long>("<xmlattr>.ss:Index",0);
									ptree::iterator CelllIter;
									long index = 0;
									map<long,string> clsVal;
									
									for(CelllIter = RowlIter->second.begin();CelllIter != RowlIter->second.end();++CelllIter)
									{
										if(CelllIter->first == "Cell")
										{
											index = CelllIter->second.get<long>("<xmlattr>.ss:Index",index+1);
											
											if(CelllIter->second.find("Data") != CelllIter->second.not_found())
											{
											
												string value = CelllIter->second.get<string>("Data");

												clsVal[index] = value;
												if(row == 0)
												{
													map<string,long>::iterator plIter;
													plIter = propertIndex.find(value);
													if(plIter != propertIndex.end())
													{
														plIter->second = index;
													}
													
													//cout<<value<<"("<<index<<")\t";
												}
												//cout<<"index:"<<index<<"=>"<<value<<endl;
											}
										}
										
									}
									if(row == 0)
									{
										head = clsVal;
										//cout<<endl;
									}
									else
									{
										map<long,string>::iterator lIter;
										for(lIter = clsVal.begin();lIter != clsVal.end();++lIter)
										{
											Dic[clsVal[propertIndex["对象"]]].sems[clsVal[propertIndex["ID"]]][head[lIter->first]] = lIter->second;
											
											if(lIter->first==propertIndex["类型"])
											{
												if(lIter->second == "请求量")
												{
													dicRequestSemIDList.push_back(&Dic[clsVal[propertIndex["对象"]]].sems[clsVal[propertIndex["ID"]]]);
												}
											}
										}
										//建立对象编码和对象的映射关系
									
										ObjTypeMap[(boost::format("%02s")%clsVal[propertIndex["对象编码"]]).str()] = clsVal[propertIndex["对象"]];
										//cout<<"编码:"<<clsVal[propertIndex["对象编码"]]<<":"<<clsVal[propertIndex["对象"]]<<endl;
									}
									++row;
								}
							}
							catch (boost::exception &e) 
							{
								//TRACE(RSTR("boost错误:")"%s\n",boost::diagnostic_information(e).c_str());//boost::diagnostic_information 会提取所有信息
							} 
							catch(std::exception& e)
							{//catch的前后顺序定义会有区别抓取的

								//TRACE(RSTR("系统错误:")"%s\n",e.what());	
							}								
						}

						list<map<string,string>*>::iterator ptlIter;
						for(ptlIter = dicRequestSemIDList.begin();ptlIter != dicRequestSemIDList.end();++ptlIter)
						{
							RegisterRequestSem((*(*ptlIter))["对象"],(*(*ptlIter))["信号名"],(*(*ptlIter))["必要的信号量"],(*(*ptlIter))["可选的信号量"]);
						}
						
						//break;
					}
					else if (tblName == "对象表")
					{
						ptree Table = rootlIter->second.get_child("Table");
						
						ptree::iterator RowlIter;
						map<long,string> head;

						map<string,long> propertIndex;
						propertIndex["对象类型"] = 0;
						propertIndex["对象编码"] = 0;
						propertIndex["解析"] = 0;
						propertIndex["数据表名"] = 0;
						
						propertIndex["文本型扩展数据库生成"] = 0;
						propertIndex["文本型扩展数据库最大长度"] = 0;
						propertIndex["数字型扩展数据库生成"] = 0;
						
						int row = 0;
						
						list<map<string,string>*> dicRequestSemIDList;
						for(RowlIter = Table.begin();RowlIter != Table.end();++RowlIter)
						{
							try
							{
								if(RowlIter->first == "Row")
								{//
									//long index = RowlIter->second.get<long>("<xmlattr>.ss:Index",0);
									ptree::iterator CelllIter;
									long index = 0;
									map<long,string> clsVal;
									
									for(CelllIter = RowlIter->second.begin();CelllIter != RowlIter->second.end();++CelllIter)
									{
										if(CelllIter->first == "Cell")
										{
											index = CelllIter->second.get<long>("<xmlattr>.ss:Index",index+1);
											if(CelllIter->second.find("Data") != CelllIter->second.not_found())
											{
												string value = CelllIter->second.get<string>("Data");

												clsVal[index] = value;
												if(row == 0)
												{
													map<string,long>::iterator plIter;
													plIter = propertIndex.find(value);
													if(plIter != propertIndex.end())
													{
														plIter->second = index;
													}
													
													//cout<<value<<"("<<index<<")\t";
												}
												//cout<<"index:"<<index<<"=>"<<value<<endl;
											}
										}
										
									}
									if(row == 0)
									{
										head = clsVal;
										//cout<<endl;
									}
									else
									{
										/*map<long,string>::iterator lIter;
										for(lIter = clsVal.begin();lIter != clsVal.end();++lIter)
										{
											Dic[clsVal[propertIndex["对象"]]].property[head[lIter->first]] = lIter->second;
											cout<<head[lIter->first]<<" ";
										}
										cout<<endl;*/
										
										map<string,long>::iterator lIter;
										if(clsVal[propertIndex["对象类型"]] != "")
										{
											for(lIter = propertIndex.begin();lIter != propertIndex.end();++lIter)
											{

												Dic[clsVal[propertIndex["对象类型"]]].property[lIter->first] = clsVal[lIter->second];
											//	cout<<clsVal[lIter->second]<<" ";
												
											}
											//cout<<endl;
										}

									}
									++row;
								}
							}
							catch (boost::exception &e) 
							{
								//TRACE(RSTR("boost错误:")"%s\n",boost::diagnostic_information(e).c_str());//boost::diagnostic_information 会提取所有信息
							} 
							catch(std::exception& e)
							{//catch的前后顺序定义会有区别抓取的

								//TRACE(RSTR("系统错误:")"%s\n",e.what());	
							}
						}
					}
						
					else if (tblName == "数据库")
					{
						ptree Table = rootlIter->second.get_child("Table");
						
						ptree::iterator RowlIter;
						map<long,string> head;

						map<string,long> propertIndex;
						propertIndex["数据库名"] = 0;
						propertIndex["是否生成数据库"] = 0;
						propertIndex["运行状态"] = 0;
						propertIndex["用户名"] = 0;
						
						propertIndex["密码"] = 0;
						propertIndex["地址"] = 0;
						propertIndex["端口"] = 0;
						
						int row = 0;

						for(RowlIter = Table.begin();RowlIter != Table.end();++RowlIter)
						{
							try
							{
								if(RowlIter->first == "Row")
								{//
									//long index = RowlIter->second.get<long>("<xmlattr>.ss:Index",0);
									ptree::iterator CelllIter;
									long index = 0;
									map<long,string> clsVal;
									
									for(CelllIter = RowlIter->second.begin();CelllIter != RowlIter->second.end();++CelllIter)
									{
										if(CelllIter->first == "Cell")
										{
											index = CelllIter->second.get<long>("<xmlattr>.ss:Index",index+1);
											if(CelllIter->second.find("Data") != CelllIter->second.not_found())
											{
												string value = CelllIter->second.get<string>("Data");

												clsVal[index] = value;
												if(row == 0)
												{
													map<string,long>::iterator plIter;
													plIter = propertIndex.find(value);
													if(plIter != propertIndex.end())
													{
														plIter->second = index;
													}
													
													//cout<<value<<"("<<index<<")\t";
												}

												//cout<<"index:"<<index<<"=>"<<value<<endl;
											}
										}
										
									}
									if(row == 0)
									{
										head = clsVal;
										//cout<<endl;
									}
									else
									{
										map<long,string>::iterator lIter;
										if(clsVal[propertIndex["是否生成数据库"]] == "是")
										{
											for(lIter = clsVal.begin();lIter != clsVal.end();++lIter)
											{
												string label = head[lIter->first];
												if(propertIndex.find(label) == propertIndex.end())
												{
													//table
													if(lIter->second == "是")
													{
														RemoteDatabaseArr[clsVal[propertIndex["数据库名"]]].tables[label] = label;
														//cout<<" "<<label;
													}
												}
												else
												{
													RemoteDatabaseArr[clsVal[propertIndex["数据库名"]]].property[label] = lIter->second;
													//cout<<" "<<lIter->second;
													//属性
												}
												//Dic[clsVal[propertIndex["对象"]]].property[head[lIter->first]] = lIter->second;
												//cout<<head[lIter->first]<<" ";
											}
											//cout<<endl;
										}
										/*cout<<endl;*/
																		
										
										

									}
									
									++row;
								}
							}
							catch (boost::exception &e) 
							{
								//TRACE(RSTR("boost错误:")"%s\n",boost::diagnostic_information(e).c_str());//boost::diagnostic_information 会提取所有信息
							} 
							catch(std::exception& e)
							{//catch的前后顺序定义会有区别抓取的

								//TRACE(RSTR("系统错误:")"%s\n",e.what());	
							}							
						}
					}
					else if(tblName == "信号类型")
					{
						ptree Table = rootlIter->second.get_child("Table");
						
						ptree::iterator RowlIter;
						map<long,string> head;

						map<string,long> propertIndex;
						propertIndex["类型"] = 0;
						propertIndex["编码"] = 0;
						
						int row = 0;
						
						for(RowlIter = Table.begin();RowlIter != Table.end();++RowlIter)
						{
							try
							{
								if(RowlIter->first == "Row")
								{
									ptree::iterator CelllIter;
									long index = 0;
									map<long,string> clsVal;
									
									for(CelllIter = RowlIter->second.begin();CelllIter != RowlIter->second.end();++CelllIter)
									{
										if(CelllIter->first == "Cell")
										{
											index = CelllIter->second.get<long>("<xmlattr>.ss:Index",index+1);
											if(CelllIter->second.find("Data") != CelllIter->second.not_found())
											{
												string value = CelllIter->second.get<string>("Data");

												clsVal[index] = value;
												if(row == 0)
												{
													map<string,long>::iterator plIter;
													plIter = propertIndex.find(value);
													if(plIter != propertIndex.end())
													{
														plIter->second = index;
													}
													
													//cout<<value<<"("<<index<<")\t";
												}

												//cout<<"index:"<<index<<"=>"<<value<<endl;
											}
										}
										
									}
									if(row == 0)
									{
										head = clsVal;
										//cout<<endl;
									}
									else
									{
										string t = clsVal[propertIndex["类型"]];										
										string id = Common::FixString(clsVal[propertIndex["编码"]],2);
										//cout<<"导入信号类型:"<<t<<":"<<id<<endl;
										SemType["类型名2ID"][t] = id;
										SemType["ID2类型名"][id] = t;
									}
									
									++row;
								}
							}
							catch (boost::exception &e) 
							{
								//TRACE(RSTR("boost错误:")"%s\n",boost::diagnostic_information(e).c_str());//boost::diagnostic_information 会提取所有信息
							} 
							catch(std::exception& e)
							{//catch的前后顺序定义会有区别抓取的

								//TRACE(RSTR("系统错误:")"%s\n",e.what());	
							}							
						}
					}
					else if (tblName == "公式")
					{
						ptree Table = rootlIter->second.get_child("Table");
						
						ptree::iterator RowlIter;
						map<long,string> head;

						map<string,long> propertIndex;
						propertIndex["操作符"] = 0;
						propertIndex["公式"] = 0;
						propertIndex["扩展数据库公式"] = 0;
						
						int row = 0;
						
						for(RowlIter = Table.begin();RowlIter != Table.end();++RowlIter)
						{
							try
							{
								if(RowlIter->first == "Row")
								{
									ptree::iterator CelllIter;
									long index = 0;
									map<long,string> clsVal;
									
									for(CelllIter = RowlIter->second.begin();CelllIter != RowlIter->second.end();++CelllIter)
									{
										if(CelllIter->first == "Cell")
										{
											index = CelllIter->second.get<long>("<xmlattr>.ss:Index",index+1);
											if(CelllIter->second.find("Data") != CelllIter->second.not_found())
											{
												string value = CelllIter->second.get<string>("Data");

												clsVal[index] = value;
												if(row == 0)
												{
													map<string,long>::iterator plIter;
													plIter = propertIndex.find(value);
													if(plIter != propertIndex.end())
													{
														plIter->second = index;
													}
													
													//cout<<value<<"("<<index<<")\t";
												}

												//cout<<"index:"<<index<<"=>"<<value<<endl;
											}
										}
										
									}
									if(row == 0)
									{
										head = clsVal;
										//cout<<endl;
									}
									else
									{
										FormulaMap[clsVal[propertIndex["操作符"]]]["公式"] = clsVal[propertIndex["公式"]];
										FormulaMap[clsVal[propertIndex["操作符"]]]["扩展数据库公式"] = clsVal[propertIndex["扩展数据库公式"]];
										//cout<<"操作符:"<<clsVal[propertIndex["操作符"]]<<" 公式:"<<clsVal[propertIndex["公式"]]<<endl;
									}
									
									++row;
								}
							}
							catch (boost::exception &e) 
							{
								TRACE(RSTR("boost错误:")"%s\n",boost::diagnostic_information(e).c_str());//boost::diagnostic_information 会提取所有信息
							} 
							catch(std::exception& e)
							{//catch的前后顺序定义会有区别抓取的

								TRACE(RSTR("系统错误:")"%s\n",e.what());	
							}							
						}
					}
				}

			}

			
			
			//遍历下字典表
		/*	map<string,map<string,map<string,string> > >::iterator _1lIter;
			for(_1lIter = Dic.begin();_1lIter != Dic.end();++_1lIter)
			{
				map<string,map<string,string> >::iterator _2lIter;
				for(_2lIter = _1lIter->second.begin();_2lIter != _1lIter->second.end();++_2lIter)
				{
					map<string,string>::iterator _3lIter;
					for(_3lIter = _2lIter->second.begin();_3lIter != _2lIter->second.end();++_3lIter)
					{
						cout<<_3lIter->second<<"\t";
					}
					
					cout<<endl;
				}
				
			}*/
			
		} 
		catch (boost::exception &e) 
		{ 

			TRACE(RSTR("boost错误:")"%s\n",boost::diagnostic_information(e).c_str());//boost::diagnostic_information 会提取所有信息

			ret = false;
		} 
		catch(std::exception& e){//catch的前后顺序定义会有区别抓取的

			TRACE(RSTR("系统错误:")"%s\n",e.what());	

			ret = false;

		}

		return ret;
	}


	
	bool Global::CreateObjects()		//建立对象
	{
		RegisterObject(boost::make_shared<CObject>(*(new CObjectUser())));
		RegisterObject(boost::make_shared<CObject>(*(new CObjectCar())));
		RegisterObject(boost::make_shared<CObject>(*(new CObjectPark())));
		RegisterObject(boost::make_shared<CObject>(*(new CObjectOrder())));
		RegisterObject(boost::make_shared<CObject>(*(new CObjectInteraction())));
		RegisterObject(boost::make_shared<CObject>(*(new CObjectArea())));
		
		
		
		
		return true;
	}
	CObject::ObjectPtr Global::GetObjectCopy(string objTypeCode)	//获取一个对象的拷贝
	{
		map<string,string>::iterator lIter;
		CObject::ObjectPtr ptr;
		lIter = ObjTypeMap.find(objTypeCode);
		if(lIter != ObjTypeMap.end())
		{
			map<string,CObject::ObjectPtr> ::iterator olIter;
			olIter = ObjectMap.find(lIter->second);
			if(olIter != ObjectMap.end())
				ptr = olIter->second;
		}
		else
		{
			cout<<RSTR("错误:")<<objTypeCode<<"对象类型编码未注册"<<endl;
			boost::throw_exception(runtime_error("对象类型编码未注册"));

		}
		return ptr;
	}
	
	void Global::RegisterObject(CObject::ObjectPtr obj)
	{
		assert(obj != NULL);
		
		//obj->LoadPropertCfg(Values["配置方案目录"]);
		ObjectMap[obj->GetObjectType()] = obj;
		
		
		
	}
	
	bool Global::RegisterRequestSem(string objType,string request,string necSem,string optSem)
	{
		//cout<<"注册："<<objType<<" "<<request<<" "<<necSem<<" "<<optSem<<endl;
		map<string,CObject::ObjectPtr>::iterator lIter;
		lIter = ObjectMap.find(objType);
		if(lIter != ObjectMap.end())
		{
			CObject::ObjectPtr pObj = lIter->second;
			assert(pObj != NULL);

			vector<string> necSemList;
			vector<string> optSemList;
			boost::split(necSemList,necSem,boost::is_any_of(";"));
			boost::split(optSemList,optSem,boost::is_any_of(";"));				

			vector<string>::iterator neclIter;
			for(neclIter = necSemList.begin();neclIter != necSemList.end();++neclIter)
			{
				string semID = MatchSemID(objType,"信号名",*neclIter);
				if(semID != "")
				{
					
					pObj->LoadDicSPID(request,"必要的信号量",semID);
				}
				
			}
			
			vector<string>::iterator optlIter;
			for(optlIter = optSemList.begin();optlIter != optSemList.end();++optlIter)
			{
				string semID = MatchSemID(objType,"信号名",*optlIter);
				if(semID != "")
				{					
					pObj->LoadDicSPID(request,"可选的信号量",semID);
				}
				
			}
			
		}
		else
		{
			cout<<YSTR("警告:")"请求量【"<<request<<"】无法被注册,因为"<<"对象类型:【"<<objType<<"】未注册"<<endl;
			return false;
		}
		
		return true;
	}
	
	string Global::MatchSemID(string objType,string attribute,string value)
	{
		string ret = "";
		//map<string,map<string,map<string,string> > >::iterator lIter;
		map<string,TDicParam>::iterator lIter;
		lIter = Dic.find(objType);
		if(lIter != Dic.end())
		{
			map<string,map<string,string> >::iterator semlIter;
			for(semlIter = lIter->second.sems.begin();semlIter != lIter->second.sems.end();++semlIter)
			{
				if(semlIter->second.find(attribute) != semlIter->second.end())
				{
					if(semlIter->second[attribute] == value)
					{
						ret =  semlIter->first;
						break;
					}
				}
			}			
		}
		
		return ret;
	}
	bool Global::CreateMySQLFile(string outFileName,string databaseName,TDatabaseParam* dbParam)
	{
		std::ofstream output(outFileName.c_str(),ios::out);
		
		if(output.good())
		{
			//string databaseName = "ParkingSpaceData";
			output<<(boost::format("drop database if exists `%s`;\n")%databaseName).str();
			output<<(boost::format("create database `%s`;	-- 创建停车位数据库\n")%databaseName).str();
			output<<(boost::format("use `%s`;\n")%databaseName).str();

#if 0
			map<string,map<string,map<string,string> > >::iterator tablelIter;
			for(tablelIter = sGlobal.DatabaseCfg.tableProperty.begin();tablelIter != sGlobal.DatabaseCfg.tableProperty.end();++tablelIter)
			{
				string tableName = ObjectMap[tablelIter->first]->GetProperty("数据表");
				if(tableName != "")
				{
					output<<(boost::format("create table %s\n(\n")%tableName).str();
					
					map<string,map<string,string> >::iterator charMaplIter;
					int i = 0;
					for(charMaplIter = tablelIter->second.begin();charMaplIter != tablelIter->second.end();++charMaplIter,++i)
					{
						if(i == tablelIter->second.size()-1)
						{
							output<<(boost::format("\t%s %s\n")%charMaplIter->second["字段"]%charMaplIter->second["字典类型"]).str();
						}
						else
						{
							output<<(boost::format("\t%s %s,\n")%charMaplIter->second["字段"]%charMaplIter->second["字典类型"]/*%charMaplIter->second["信号量"]%charMaplIter->second["解释"]*/).str();
						}
					}
					output<<")character set = utf8;\n";
					output<<(boost::format("create table %sExt\n(\n")%tableName).str();
					output<<"\tid int unsigned primary key not null auto_increment,\n";
					output<<"\tmatchID int unsigned not null,\n";
					output<<"\tsemSN int unsigned not null,\n";
					output<<"\tsemSerial tinyint unsigned not null,\n";
					output<<"\tval varchar(20)\n";
					output<<")character set = utf8;\n";
				}
			}
#else
			map<string,TDicParam>::iterator lIter;
			for(lIter = sGlobal.Dic.begin();lIter != sGlobal.Dic.end();++lIter)
			{
				string tableName = lIter->second.property["数据表名"];
				//cout<<"生成数据表:"<<tableName<<endl;
				if(tableName != "")
				{
					if(dbParam != NULL)
					{
						string objName = lIter->first;
						if(dbParam->tables.find(objName) == dbParam->tables.end())
						{
							//cout<<"找不到表名:"<<objName<<endl;
							continue;
						}
					}
					output<<(boost::format("create table %s\n(\n")%tableName).str();
					
					map<string,map<string,string> >::iterator charMaplIter;
					int i = 0;
					for(charMaplIter = lIter->second.sems.begin();charMaplIter != lIter->second.sems.end();++charMaplIter,++i)
					{
						string type = charMaplIter->second["类型"];
						
						if(type == "索引量"
							|| type =="记录量"
						)
						{
							
							if(charMaplIter->second["字段名"] != "" 
							&& charMaplIter->second["字段类型"] != "")
							{
								
								if(i > 0)
								{
									output<<",";
									output<<endl;
								}
								
								output<<"\t"<<charMaplIter->second["字段名"]<<" "<<charMaplIter->second["字段类型"];
								
								if(charMaplIter->second["是否非空"] == "是")
								{
									output<<" not null";
								}
								if(charMaplIter->second["是否唯一"] == "是")
								{
									output<<" unique";
								}
								if(charMaplIter->second["是否关键字段"] == "是")
								{
									output<<" primary key";
								}
								if(charMaplIter->second["是否自增"] == "是")
								{
									output<<" auto_increment";
								}									
								
							}
							else
							{
								cout<<"字段名:["<<charMaplIter->second["字段名"]<<"]"<<endl;
								cout<<"字段类型:["<<charMaplIter->second["字段类型"]<<"]"<<endl;
							}
						}						
					}
					output<<"\n)character set = utf8;\n";
					
					if(lIter->second.property["文本型扩展数据库生成"] == "是")
					{					
						output<<(boost::format("create table %sTextExt\n(\n")%tableName).str();
						output<<"\tid bigint unsigned primary key not null auto_increment,\n";
						output<<"\tmatchID bigint unsigned not null,\n";
						output<<"\tsemSN int unsigned not null,\n";
						output<<"\tsemSerial tinyint unsigned not null,\n";
						output<<"\tval varchar("<<lIter->second.property["文本型扩展数据库最大长度"]<<")\n";
						output<<")character set = utf8;\n";
					}
					
					if(lIter->second.property["数字型扩展数据库生成"] == "是")
					{					
						output<<(boost::format("create table %sNumExt\n(\n")%tableName).str();
						output<<"\tid bigint unsigned primary key not null auto_increment,\n";
						output<<"\tmatchID bigint unsigned not null,\n";
						output<<"\tsemSN int unsigned not null,\n";
						output<<"\tsemSerial tinyint unsigned not null,\n";
						output<<"\tval double\n";
						output<<")character set = utf8;\n";
					}
				}
			}
	
#endif
			
			output.close();
		}
		return true;
	}
	
	bool Global::GetDatabasePtr(string key,DatabasePtr& dataPtr )
	{
		map<string,TDatabaseParam >::iterator lIter;
		lIter = RemoteDatabaseArr.find(key);
		if(lIter == RemoteDatabaseArr.end())
		{
			//从远端获取数据库信息
			//如果获取到信息
			//读取信息
			cout<<"没有找到数据库关键字:"<<key<<endl;
			return false;
		}
		
		dataPtr = boost::make_shared<CDatabaseMysql>(*(new CDatabaseMysql()));
		
		if(!dataPtr->Connect(lIter->second.property["用户名"],lIter->second.property["密码"],lIter->second.property["数据库名"],lIter->second.property["地址"]))
		{
			cout<<"连接失败:"<<key<<endl;
			return false;
		}
		
		return true;
	}
	
	bool Global::GetDatabaseCfg(string dFile)
	{
		vector<path> v;
		Common::FindFiles(dFile,".json",v);
		BOOST_FOREACH(path &p,v)
		{
			try 
			{
				string name = p.stem().string();
				string path = p.string();
				boost::property_tree::ptree root;			
				boost::property_tree::read_json(path,root);
				RemoteDatabaseArr[name].property["用户名"] = root.get<string>("用户名");
				RemoteDatabaseArr[name].property["密码"] = root.get<string>("密码");
				RemoteDatabaseArr[name].property["数据库"] = root.get<string>("数据库");
				RemoteDatabaseArr[name].property["网址"] = root.get<string>("网址");
				RemoteDatabaseArr[name].property["端口"] = root.get<string>("端口");
			}
			catch (boost::exception &e) 
			{ 

				TRACE("%s\n",boost::diagnostic_information(e).c_str());//boost::diagnostic_information 会提取所有信息
		 
			} 
			catch(std::exception& e){//catch的前后顺序定义会有区别抓取的

				TRACE("%s\n",e.what());	
			}
			//cout<<name<<endl;
		}
	}
	
	bool Global::GetDicInfo(string objType,string semID,string searchParam,string& rst)
	{
		//map<string,map<string,map<string,string> > >::iterator lIter;
		map<string,TDicParam>::iterator lIter;
		lIter = Dic.find(objType);
		if(lIter != Dic.end())
		{
			string index;
			
			stringstream ss;
			ss.width(SEM_SERIAL_LENGTH);
			ss.fill('0');
			ss<<1;
			ss>>index;
			semID = semID.substr(0,SEM_SN_LENGTH) + index;
			map<string,map<string,string> >::iterator semlIter = lIter->second.sems.find(semID);
			if(semlIter != lIter->second.sems.end())
			{
				map<string,string>::iterator tlIter = semlIter->second.find(searchParam);
				if(tlIter != semlIter->second.end())
				{
					rst = tlIter->second;
					return true;
				}
			}
		}		
		
		return false;
	}
}